from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Answer import Answer

class Unit10:
    def __init__(self, driver):
        self.Chapter10(driver)

    def Chapter10(self, driver):
        answer = Answer()
        # 点击第十单元的学习
        label = driver.find_element(By.CSS_SELECTOR, '#unit61 > div.book_unit_btn_div > button:nth-child(2)')
        driver.execute_script("arguments[0].click();", label)

        # 开始

        # 读取答案并填入
        file_name = 'Unit10Answer.txt'
        with open(file_name) as file_objECt:
            lines = file_objECt.readlines()

        # c1p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a').click()
        answers = [lines[0], lines[1], lines[2]]
        answer.vacancy_question(driver, answers, 1, 3)

        # c2t1n1
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter185"]')))
        driver.find_element(By.XPATH, '//*[@id="chapter185"]').click()
        answers = lines[3].split()
        counts = [1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t1n2
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[4].split()
        counts = [2, 2]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t1n3
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[3]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[3]/a').click()
        answers = lines[5].split()
        counts = [3, 3, 1]
        answer.choice_question(driver, answers, 3, counts, 0)

        # c2t2c1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[6].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t2c2
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answer.submit(driver, 2)

        # c2t3p1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[8], lines[9]]
        answer.vacancy_question(driver, answers, 1, 2)

        # c2t3p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[10].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t3p3
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[3]/div[2]/p[5]/input').send_keys(lines[11])
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[3]/div[2]/p[6]/input').send_keys(lines[12])
        answer.submit(driver, 3)

        # c2t3p4
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(4) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[13].split()
        counts = [2, 2, 2, 2, 2]
        answer.choice_question(driver, answers, 4, counts, 0)

        # c2t3p5
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(5) > a')
        driver.execute_script("arguments[0].click();", label)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[5]/div[2]/p[5]/input').send_keys(lines[14])
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[5]/div[2]/p[7]/input').send_keys(lines[15])
        answer.submit(driver, 5)

        # c2t3p6
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(6) > a')
        driver.execute_script("arguments[0].click();", label)
        answer.submit(driver, 6)

        # c5
        label = driver.find_element(By.CSS_SELECTOR, '#chapter188')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[17], lines[18], lines[19], lines[20], lines[21], lines[22], lines[23], lines[24], lines[25], lines[26]]
        answer.vacancy_question(driver, answers, 1, 10)

        # c6
        label = driver.find_element(By.CSS_SELECTOR, '#chapter189')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[27], lines[28], lines[29], lines[30], lines[31], lines[32], lines[33], lines[34], lines[35], lines[36]]
        answer.vacancy_question(driver, answers, 1, 10)

        # c7t1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter1451')
        driver.execute_script("arguments[0].click();", label)
        answer.submit(driver, 1)

        # c7t2
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[38], lines[39], lines[40], lines[41], lines[42], lines[43], lines[44], lines[45], lines[46], lines[47], lines[48]]
        answer.vacancy_question(driver, answers, 1, 11)

        driver.back()